package com.group02.community.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group02.community.entity.owner.ZyOwnerRoom;
import com.group02.community.entity.owner.ZyOwnerRoomRecord;
import com.group02.community.mapper.OwnerRoomMapper;
import com.group02.community.mapper.OwnerRoomRecordMapper;
import com.group02.community.service.OwnerRoomRecordService;
import com.group02.community.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OwnerRoomRecordServiceImpl extends ServiceImpl<OwnerRoomRecordMapper, ZyOwnerRoomRecord> implements OwnerRoomRecordService {
    @Autowired
    private OwnerRoomRecordMapper ownerRoomRecordMapper;
    @Autowired
    private OwnerRoomMapper ownerRoomMapper;

    @Override
    public List<ZyOwnerRoomRecord> getRecordByRoomId(String roomId) {
        return ownerRoomRecordMapper.getRecordByRoomId(roomId);
    }

    @Override
    public void addRecord(ZyOwnerRoomRecord zyOwnerRoomRecord) {
        String ownerRoomId = zyOwnerRoomRecord.getOwnerRoomId();
        ZyOwnerRoom byRoomId = ownerRoomMapper.getByOwnerRoomId(ownerRoomId);
        zyOwnerRoomRecord.setRoomId(byRoomId.getRoomId());
        zyOwnerRoomRecord.setRecordId(UUIDUtils.getId());
        zyOwnerRoomRecord.setUpdateTime(LocalDateTime.now());
        zyOwnerRoomRecord.setUnitId(byRoomId.getUnitId());
        zyOwnerRoomRecord.setCommunityId(byRoomId.getCommunityId());
        zyOwnerRoomRecord.setBuildingId(byRoomId.getBuildingId());
        zyOwnerRoomRecord.setOwnerRoomId(String.valueOf(byRoomId.getOwnerRoomId()));
        zyOwnerRoomRecord.setOwnerType(byRoomId.getOwnerType());
        ownerRoomMapper.updateOwnerRoomByRoomId(String.valueOf(byRoomId.getRoomId()) ,zyOwnerRoomRecord.getRoomStatus());
        ownerRoomRecordMapper.insert(zyOwnerRoomRecord);
    }
}
